//
//  ZHAvatarsView.m
//  Pod
//
//  Created by green on 24/10/2017.
//  Copyright © 2017 zhihan. All rights reserved.
//

#import "ZHAvatarsView.h"
#import "ZHAvatarView.h"
#import <BCComConfigKit/BCComConfigKit.h>

#import <BCFoundation/BCFoundationUtils.h>
#import <BCUIKit/UIView+ZHHelper.h>

#define kZHAvatarsView_SplitHeight      0.8

@interface ZHAvatarsView()
@property (strong, nonatomic) NSMutableArray<ZHAvatarView *>    *avatarViews;//头像view
@end

@implementation ZHAvatarsView
//MARK: - system

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = [UIColor whiteColor];
    _avatarViews = [[NSMutableArray alloc] init];
    _maxAvatar = 4;
    _suffixCount = 1;
    self.layer.masksToBounds = YES;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _avatarViews = [[NSMutableArray alloc] init];
        _maxAvatar = 4;
        _suffixCount = 1;
        self.layer.masksToBounds = YES;
    }
    return self;
}
- (void)dealloc
{
    _avatarViews = nil;
    BCLogDealloc();
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.layer.cornerRadius = self.bc_width/2.0;
}

//MARK: - 添加头像
- (void)addAvatar:(NSString *)avatar andName:(NSString *)name
{
    if(avatar.length<=0 && name.length<=0){
        return;
    }
    if(_avatarViews.count>=_maxAvatar){
        return;
    }
    ZHAvatarView *avatarView = [[ZHAvatarView alloc] initWithFrame:CGRectMake(0, 0, self.bc_width, self.bc_height)];
    avatarView.showCorner = NO;
    avatarView.suffixCount = self.suffixCount;
    avatarView.showBorder = NO;
    avatarView.clipsToBounds = YES;
    [avatarView configAvatar:avatar andName:name];
    [self addSubview:avatarView];
    [_avatarViews addObject:avatarView];
    
}
- (void)addAvatarImg:(UIImage *)avatarImg andName:(NSString *)name
{
    if(!avatarImg && name.length<=0){
        return;
    }
    if(_avatarViews.count>=_maxAvatar){
        return;
    }

    ZHAvatarView *avatarView = [[ZHAvatarView alloc] initWithFrame:CGRectMake(0, 0, self.bc_width, self.bc_height)];
    avatarView.showCorner = NO;
    avatarView.suffixCount = self.suffixCount;
    avatarView.showBorder = NO;
    avatarView.clipsToBounds = YES;
    [avatarView configAvatarImg:avatarImg andName:name];
    [self addSubview:avatarView];
    [_avatarViews addObject:avatarView];
}
- (void)clearAllAvatar
{
    [_avatarViews enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [_avatarViews removeAllObjects];
}

//MARK: - 显示
- (void)show
{
    if(_avatarViews.count<=0){
        return;
    }
    CGFloat itemWidth = 0;//item 宽度
    CGFloat itemHeight = 0;//item 宽度
    switch (_avatarViews.count) {
        case 1:
        {
            _avatarViews[0].bc_x = 0;
            _avatarViews[0].bc_y = 0;
            _avatarViews[0].bc_size = self.bc_size;
        }
            break;
        case 2:
        {
            itemWidth = CGRectGetWidth(self.frame)/2.0;
            itemHeight = CGRectGetWidth(self.frame);
            _avatarViews[0].bc_size = CGSizeMake(itemWidth, itemHeight);//{0.5,1}
            _avatarViews[0].bc_x = 0-kZHAvatarsView_SplitHeight/2.0;
            _avatarViews[0].bc_y = 0;
            _avatarViews[0].imgFrame = CGRectMake(0-itemWidth/2.0, 0, itemHeight, itemHeight);
            _avatarViews[0].titleFrame = CGRectMake(0-itemWidth/2.0, 0, itemHeight, itemHeight);
            
            _avatarViews[1].bc_size = CGSizeMake(itemWidth, itemHeight);//{0.5,1}
            _avatarViews[1].bc_x = itemWidth+kZHAvatarsView_SplitHeight/2.0;
            _avatarViews[1].bc_y = 0;
            _avatarViews[1].imgFrame = CGRectMake(0-itemWidth/2.0, 0, itemHeight, itemHeight);
            _avatarViews[1].titleFrame = CGRectMake(0-itemWidth/2.0, 0, itemHeight, itemHeight);
        }
            break;
        case 3:
        {
            itemWidth = CGRectGetWidth(self.frame)/2.0;
            itemHeight = CGRectGetWidth(self.frame);
            _avatarViews[0].bc_size = CGSizeMake(itemWidth, itemHeight);//{0.5,1}
            _avatarViews[0].bc_x = 0-kZHAvatarsView_SplitHeight/2.0;
            _avatarViews[0].bc_y = 0;
            _avatarViews[0].imgFrame = CGRectMake(0-itemWidth/2.0, 0, itemHeight, itemHeight);
            _avatarViews[0].titleFrame = CGRectMake(0-itemWidth/2.0, 0, itemHeight, itemHeight);
            
            _avatarViews[1].bc_size = CGSizeMake(itemWidth, itemWidth);//{0.5,0.5}
            _avatarViews[1].bc_x = _avatarViews[0].bc_right+kZHAvatarsView_SplitHeight;
            _avatarViews[1].bc_y = 0-kZHAvatarsView_SplitHeight/2.0;
            _avatarViews[1].imgFrame = CGRectMake(0, 0, itemWidth, itemWidth);
            _avatarViews[1].titleFrame = CGRectMake(0-itemWidth/8.0, itemWidth/8.0, itemWidth, itemWidth);//文字 左下 偏移 1/8
            [_avatarViews[1].placeholderLb setFont:BCComConfig.config.bcFont(12)];
            
            _avatarViews[2].bc_size = CGSizeMake(itemWidth, itemWidth);//{0.5,0.5}
            _avatarViews[2].bc_x = _avatarViews[1].bc_x;
            _avatarViews[2].bc_y = _avatarViews[1].bc_bottom + kZHAvatarsView_SplitHeight;
            _avatarViews[2].imgFrame = CGRectMake(0, 0, itemWidth, itemWidth);
            _avatarViews[2].titleFrame = CGRectMake(0-itemWidth/8.0, 0-itemWidth/8.0, itemWidth, itemWidth);//文字 左上 偏移 1/8
            [_avatarViews[2].placeholderLb setFont:BCComConfig.config.bcFont(12)];
        }
            break;
        case 4:
        {
            itemWidth = CGRectGetWidth(self.frame)/2.0;
            _avatarViews[0].bc_size = CGSizeMake(itemWidth, itemWidth);//{0.5,0.5}
            _avatarViews[0].bc_x = 0-kZHAvatarsView_SplitHeight/2.0;
            _avatarViews[0].bc_y = 0-kZHAvatarsView_SplitHeight/2.0;
            _avatarViews[0].imgFrame = CGRectMake(0, 0, itemWidth, itemWidth);
            _avatarViews[0].titleFrame = CGRectMake(itemWidth/8.0, itemWidth/8.0, itemWidth, itemWidth);//文字 右下 偏移 1/8
            [_avatarViews[0].placeholderLb setFont:BCComConfig.config.bcFont(12)];
            
            _avatarViews[1].bc_size = CGSizeMake(itemWidth, itemWidth);//{0.5,0.5}
            _avatarViews[1].bc_x = _avatarViews[0].bc_x;
            _avatarViews[1].bc_y = _avatarViews[0].bc_bottom + kZHAvatarsView_SplitHeight;
            _avatarViews[1].imgFrame = CGRectMake(0, 0, itemWidth, itemWidth);
            _avatarViews[1].titleFrame = CGRectMake(itemWidth/8.0, 0-itemWidth/8.0, itemWidth, itemWidth);//文字 右上 偏移 1/8
            [_avatarViews[1].placeholderLb setFont:BCComConfig.config.bcFont(12)];
            
            _avatarViews[2].bc_size = CGSizeMake(itemWidth, itemWidth);//{0.5,0.5}
            _avatarViews[2].bc_x = _avatarViews[0].bc_right+kZHAvatarsView_SplitHeight;
            _avatarViews[2].bc_y = _avatarViews[0].bc_y;
            _avatarViews[2].imgFrame = CGRectMake(0, 0, itemWidth, itemWidth);
            _avatarViews[2].titleFrame = CGRectMake(0-itemWidth/8.0, itemWidth/8.0, itemWidth, itemWidth);//文字 左下 偏移 1/8
            [_avatarViews[2].placeholderLb setFont:BCComConfig.config.bcFont(12)];
            
            _avatarViews[3].bc_size = CGSizeMake(itemWidth, itemWidth);//{0.5,0.5}
            _avatarViews[3].bc_x = _avatarViews[2].bc_x;
            _avatarViews[3].bc_y = _avatarViews[1].bc_y;
            _avatarViews[3].imgFrame = CGRectMake(0, 0, itemWidth, itemWidth);
            _avatarViews[3].titleFrame = CGRectMake(0-itemWidth/8.0, 0-itemWidth/8.0, itemWidth, itemWidth);//文字 左上 偏移 1/8
            [_avatarViews[3].placeholderLb setFont:BCComConfig.config.bcFont(12)];
        }
            break;
        default:
            break;
    }
}
@end
